package cn.edu.fudan.data;

import cn.edu.fudan.type.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Integer.parseInt;

public class ReadData {
	
	public List<DataItem> readDataFromFile(String FilePath, boolean positiveFlag) throws IOException{
		List<DataItem> data = new ArrayList<>();		
		BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
		String s = bufferedReader.readLine();
		double max = 0;
		long point = 0;
		while(s!=null){
			DataItem di = new DataItem();
			di.setTimestamp(Long.parseLong(s.split("\t")[0]));
			di.setValue(Double.parseDouble(s.split("\t")[1]));
			if(di.getValue() > max){
				max = di.getValue();
				point = di.getTimestamp();
			}
			if(positiveFlag){
				if(di.getValue() >= 0)	data.add(di);
				if(di.getValue() < 0){
					di.setValue(0);
					data.add(di);
				}
			}else{
				if(di.getValue() < 0){
					di.setValue(-di.getValue());
					data.add(di);
				}
			}
			s = bufferedReader.readLine();
		}
//		System.out.println(point);
		bufferedReader.close();
		return data;
	}
	
	public List<DataItem> readDataFromFile(String FilePath) throws IOException{
		List<DataItem> data = new ArrayList<>();		
		BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
		String s = bufferedReader.readLine();
		while(s!=null){
			DataItem di = new DataItem();
			di.setTimestamp(Long.parseLong(s.split("\t")[0]));
			di.setValue(Double.parseDouble(s.split("\t")[1]));	
			data.add(di);
			s = bufferedReader.readLine();
		}
		bufferedReader.close();
		return data;
	}
	
	public List<DataItem> readPAMAPDataFromFile(String FilePath, boolean positiveFlag, PAMAPDimension dimension) throws IOException{
		List<DataItem> data = new ArrayList<>();
		BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
		String s = bufferedReader.readLine();
		while(s!=null){
			DataItem di = new DataItem();
			di.setTimestamp((long) (10000*Double.parseDouble(s.split("\\s")[1])));
			di.setValue(Double.parseDouble(s.split("\\s")[dimension.ordinal()+2]));			
			if(positiveFlag){
				if(di.getValue() >= 0)	data.add(di);
				if(di.getValue() < 0){
					di.setValue(0);
					data.add(di);
				}
			}else{
				if(di.getValue() >= 0){
					di.setValue(0);
					data.add(di);
				}
				if(di.getValue() < 0){
					di.setValue(-di.getValue());
					data.add(di);
				}

			}
			s = bufferedReader.readLine();
		}
		bufferedReader.close();
		return data;
	}
	
	public List<DataItem> readPAMAPDataFromFile(String FilePath, PAMAPDimension dimension) throws IOException{
		List<DataItem> data = new ArrayList<>();
		BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
		String s = bufferedReader.readLine();
		while(s!=null){
			DataItem di = new DataItem();
			di.setTimestamp((long) (10000*Double.parseDouble(s.split("\\s")[1])));
			di.setValue(Double.parseDouble(s.split("\\s")[dimension.ordinal()+2]));			
			data.add(di);
			s = bufferedReader.readLine();
		}
		bufferedReader.close();
		return data;
	}
	
	public BeginEndTime readPAMAPTimeFile(String FilePath, Activities lable) throws IOException{
		BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
		String s = bufferedReader.readLine();
		int flag = 0;
		double begin = 0;
		double end = 0;
		while(s!=null){
			int dimension = parseInt(s.split("\\s")[0]);
			if(dimension == lable.ordinal()){
				end = Double.parseDouble(s.split("\\s")[1]);
				if(flag == 0){
					begin = end;
				}
				flag = 1;
			}
			if(flag == 1 && dimension != lable.ordinal()){
				break;
			}
			s = bufferedReader.readLine();
		}
		BeginEndTime bet = new BeginEndTime();
		bet.setBegin(begin);
		bet.setEnd(end);		
		bufferedReader.close();
		return bet;
	}
	
	public List<List<Long>> readTimePoint(String FilePath, boolean fix) throws Exception{
		List<List<Long>> timepoints = new ArrayList<>();
		BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
		String s = bufferedReader.readLine();
		while(s!=null){
			List<Long> timepoint = new ArrayList<>();
			String[] time = s.split("\\s");
			for(int i = 0; i < time.length; i ++){
				if(i == 0) continue;
				if(fix){
					timepoint.add((long)Double.parseDouble(time[i])*10000);
				}
				else{
					timepoint.add((long)Double.parseDouble(time[i]));
				}
			}
			if(timepoint.size() > 0){
				timepoints.add(timepoint);
			}
			s = bufferedReader.readLine();
		}
		bufferedReader.close();
		return timepoints;
	}

	public TwoTuple<List<List<DataItem>>,Integer> readTimeFromUCRFormat(String FilePath)throws Exception{ //从ucr数据集读数据,返回一个序列的集合和类的数量
		List<List<DataItem>> data = new ArrayList<>();
		Integer Nclass =0;
		List<Integer> classlabel= new ArrayList<>();
		BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
		String s = bufferedReader.readLine();
		while(s!=null){
			List<DataItem> di = new ArrayList<>();
			int timestamp =0;
			Integer t=Integer.parseInt(s.split(",")[timestamp]);
			if(!classlabel.contains(t)){  //第一个是类标签
				Nclass++;
			}
			//timestamp++;
			for(;timestamp<s.split(",").length;timestamp++){
				DataItem value =new DataItem();
				value.setTimestamp(timestamp);
				value.setValue(Double.parseDouble(s.split(",")[timestamp]));
				di.add(value);
			}
			//System.out.println(di);
			data.add(di);
			s = bufferedReader.readLine();
		}
		bufferedReader.close();
		return new TwoTuple<List<List<DataItem>>,Integer>(data,Nclass);
	}
}
